<?php

namespace app\home\home;
class Ceshialipy{
    public function _initialize() {
        Loader::import('PAlipay.aop.AopClient', EXTEND_PATH);
        Loader::import('PAlipay.aop.request.AlipayTradePagePayRequest', EXTEND_PATH);
        Loader::import('PAlipay.aop.request.AlipayTradeQueryRequest', EXTEND_PATH);
        Loader::import('PAlipay.aop.request.AlipayNotify', EXTEND_PATH);
    }

    //支付宝支付二维码获取页面
    public function useAliPay($order_info){
        //商户订单号，商户网站订单系统中唯一订单号，必填
        $out_trade_no =$order_info['out_trade_no'];
        //订单名称，必填
        $proName = $order_info['subject'];
        //付款金额，必填
        $total_amount = $order_info['total_amount'];//trim($_POST['WIDtotal_amount']);
        //商品描述，可空
        $body = '';//trim($_POST['WIDbody']);
        //请求
        $c = new \AopClient();
        $c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $c->appId = config('alipay_config.app_id');
        $c->rsaPrivateKey = config('alipay_config.rsa_private_key');
        $c->format = 'json';
        $c->charset= 'UTF-8';
        $c->signType= 'RSA2';
        $c->alipayrsaPublicKey = config('alipay_config.open_public_key');
        $request = new \AlipayTradePagePayRequest();
        $request->setReturnUrl(config('return_url'));
        $request->setNotifyUrl(config('notify_url'));
        $request->setBizContent("{" .
            "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," .
            "    \"subject\":\"$proName\"," .
            "    \"out_trade_no\":\"$out_trade_no\"," .
            "    \"total_amount\":$total_amount," .
            "    \"body\":\"$body\"" .
            "  }");
        $result = $c->pageExecute($request);
        return $result;
    }


    /**
     * @throws \Exception
     */
    public function refund(){
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config('alipay_config.app_id');
        $aop->rsaPrivateKey = config('alipay_config.rsa_private_key');
        $aop->alipayrsaPublicKey=config('alipay_config.open_public_key');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA';
        $aop->postCharset='GBK';
        $aop->format='json';
        $request = new \AlipayTradeRefundRequest();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"20150320010101001\"," .
            "\"trade_no\":\"2014112611001004680073956707\"," .
            "\"refund_amount\":200.12," .
            "\"refund_reason\":\"正常退款\"," .
            "\"out_request_no\":\"HZ01RF001\"," .
            "\"operator_id\":\"OP001\"," .
            "\"store_id\":\"NJ_S_001\"," .
            "\"terminal_id\":\"NJ_T_001\"" .
            "  }");
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }

    public function orderqueryno($out_trade_no){
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config('alipay_config.app_id');
        $aop->rsaPrivateKey = config('alipay_config.rsa_private_key');
        $aop->alipayrsaPublicKey=config('alipay_config.open_public_key');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $request = new \AlipayTradeQueryRequest ();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"$out_trade_no\"," .
            "\"trade_no\":\"\"" .
            "  }");
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            return $result->$responseNode;
        } else {
            echo "失败";
        }
    }

    /**
     * @func 支付宝异步回调接口
     */
    function notifyurl(){
        $alipay_config=C('alipay');
        //计算得出通知验证结果
        $alipayNotify = new \AlipayNotify($alipay_config);
        $verify_result = $alipayNotify->verifyNotify();
        if($verify_result) {

        }

    }

}